home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / extra / getfnl.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  103 lines

  1.  
  2. /*
  3.  *  GETFNL.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <libraries/dos.h>
  12. #include <clib/dos_protos.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <lib/misc.h>
  17.  
  18. typedef struct FileInfoBlock FIB;
  19.  
  20. int
  21. getfnl(pat, buf, bufSize, attr)
  22. const char *pat;
  23. char *buf;
  24. size_t bufSize;
  25. int attr;        /*  0 = files, 1 = files & dirs */
  26. {
  27.     FIB *fib = malloc(sizeof(FIB));
  28.     int r = -1;
  29.     void *wildNode = NULL;
  30.  
  31.     errno = 0;
  32.  
  33.     _SetWildStack(2048);
  34.  
  35.     --bufSize;        /*    final \0 at end */
  36.  
  37.     if (fib) {
  38.     const char *ptr;
  39.     BPTR lock = NULL;
  40.  
  41.     for (ptr = pat + strlen(pat); ptr >= pat; --ptr) {
  42.         if (*ptr == '/' || *ptr == ':')
  43.         break;
  44.     }
  45.     ++ptr;        /*    points to just after the last / or :,    */
  46.             /*    or to the beginning if no / or :    */
  47.     /*
  48.      *  can't modify a const string !
  49.      */
  50.     wildNode = _ParseWild(ptr, strlen(ptr));
  51.  
  52.     {
  53.         short len = ptr - pat;
  54.         char *hdr = malloc(len + 1);
  55.  
  56.         if (hdr)
  57.         {
  58.                 strncpy(hdr, pat, len);
  59.                 hdr[len] = 0;
  60.  
  61.                 lock = Lock(hdr, SHARED_LOCK);
  62.                 free(hdr);
  63.         }
  64.     }
  65.  
  66.     if (lock) {
  67.         if (Examine(lock, fib)) {
  68.         r = 0;
  69.         while (ExNext(lock, fib)) {
  70.             short len;
  71.             short prelen = ptr - pat;
  72.  
  73.             if (attr == 0 && fib->fib_DirEntryType > 0)
  74.             continue;
  75.             if (_CompWild(fib->fib_FileName, wildNode, NULL) < 0)
  76.             continue;
  77.             if (errno)
  78.             break;
  79.             len = strlen(fib->fib_FileName) + prelen + 1;
  80.             if (len > bufSize) {
  81.             r = -1;
  82.             break;
  83.             }
  84.             strncpy(buf, pat, prelen);
  85.             strcpy(buf + prelen, fib->fib_FileName);
  86.             buf += len;
  87.             bufSize -= len;
  88.             ++r;
  89.         }
  90.         }
  91.         UnLock(lock);
  92.     }
  93.     free(fib);
  94.     }
  95.     _FreeWild(wildNode);
  96.     if (bufSize > 0)
  97.     *buf = 0;
  98.     if (errno)
  99.     r = -1;
  100.     return(r);
  101. }
  102.  
  103.